//
// Created by liaoyinyu on 2021/8/15.
//

#include "page1.h"
#include "param.h"

#if 0
/******************************************************************************
  Function:     VentilationCurve_Display
  Description:  通风曲线界面数据显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void VentilationCurve_Display(void)
{
    char string_tmp[20];
//SetT,bandwidth,MaxvAntilation,MinvAntilation  Run_F,Antilation Refresh

    sprintf(string_tmp, "%.1f", g_tParam.SetT );//保留小数点1位
    LCD_Txt(1, 2, string_tmp); //设置温度
    sprintf(string_tmp, "%.1f", g_tParam.bandwidth);//保留小数点1位
    LCD_Txt(1, 3, string_tmp); //带宽
    sprintf(string_tmp, "%.1f", g_tParam.MaxVntilation);//保留小数点1位
    LCD_Txt(1, 4, string_tmp); //最大通风量
    sprintf(string_tmp, "%.1f", g_tParam.MinVntilation);//保留小数点1位
    LCD_Txt(1, 5, string_tmp); //最小通风量
    sprintf(string_tmp, "%.1f", ADC_Value[4]);//保留小数点1位
    LCD_Txt(1, 6, string_tmp); //温度
    sprintf(string_tmp, "%.1f", Antilation);//保留小数点1位
    LCD_Txt(1, 7, string_tmp); //当前通风量

    sprintf(string_tmp, "%.1f", g_tParam.SetT);//保留小数点1位
    LCD_Txt(1, 10, string_tmp); //设置温度
    sprintf(string_tmp, "%.1f", g_tParam.bandwidth);//保留小数点1位
    LCD_Txt(1, 12, string_tmp); //带宽
    sprintf(string_tmp, "%.1f", g_tParam.MaxVntilation);//保留小数点1位
    LCD_Txt(1, 8, string_tmp); //最大通风量
    sprintf(string_tmp, "%.1f", g_tParam.MinVntilation);//保留小数点1位
    LCD_Txt(1, 9, string_tmp); //最小通风量
    sprintf(string_tmp, "%.1f", g_tParam.bandwidth+g_tParam.SetT);//保留小数点1位
    LCD_Txt(1, 11, string_tmp); //带宽

}



/******************************************************************************
  Function:     Debug_Display
  Description:  调试窗口显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void Debug_Display(void)
{
    char string_tmp[20];

    sprintf(string_tmp, "%.1f", g_tParam.MaxVntilation);//保留小数点1位
    LCD_Txt(5, 2, string_tmp); //最大通风量
    sprintf(string_tmp, "%.1f", g_tParam.MinVntilation);//保留小数点1位
    LCD_Txt(5, 3, string_tmp); //最小通风量
    sprintf(string_tmp, "%.1f", g_tParam.SetT);//保留小数点1位
    LCD_Txt(5, 4, string_tmp); //设置温度
    sprintf(string_tmp, "%.1f", g_tParam.bandwidth);//保留小数点1位
    LCD_Txt(5, 5, string_tmp); //带宽
//	sprintf(string_tmp, "%.1f", Output_F*10);//保留小数点1位
//	LCD_Txt(5, 6, string_tmp); //目标通风量
//	sprintf(string_tmp, "%.1f", Antilation);//保留小数点1位
//	LCD_Txt(5, 7, string_tmp); //当前通风量

    sprintf(string_tmp, "%d", g_tParam.Refresh);//保留小数点1位
    LCD_Txt(5, 8, string_tmp); //刷新频率
    sprintf(string_tmp, "%d", Update_time);//保留小数点1位
    LCD_Txt(5, 9, string_tmp); //更正通风倒计时
    sprintf(string_tmp, "%.1f", ADC_Value[4]);//保留小数点1位
    LCD_Txt(5, 10, string_tmp); //温度

    sprintf(string_tmp, "%.1f", yk);//保留小数点1位
    LCD_Txt(5, 11, string_tmp); //yk
//	sprintf(string_tmp, "%.1f", Output_F);//保留小数点1位
//	LCD_Txt(5, 12, string_tmp); //目标输出电压
//	sprintf(string_tmp, "%.1f", Run_F);//保留小数点1位yk Run_F
//	LCD_Txt(5, 13, string_tmp); //当前运行频率


    sprintf(string_tmp, "%.1f", Output_F*10);//保留小数点1位
    LCD_Txt(5, 6, string_tmp); //目标通风量
    sprintf(string_tmp, "%.1f", Antilation);//保留小数点1位
    LCD_Txt(5, 7, string_tmp); //当前通风量
    sprintf(string_tmp, "%.1f", Run_F);//保留小数点1位yk Run_F
    LCD_Txt(5, 13, string_tmp); //当前运行频率
    sprintf(string_tmp, "%.1f", yk);//保留小数点1位
    LCD_Txt(5, 14, string_tmp); //当前输出电压	yk
    sprintf(string_tmp, "%.1f", Output_F);//保留小数点1位
    LCD_Txt(5, 12, string_tmp); //目标输出电压

    sprintf(string_tmp, "%.1f", ADC_Value[5]);//保留小数点1位
    LCD_Txt(5, 15, string_tmp); //湿度
    sprintf(string_tmp, "%.1f", ADC_Value[7]);//保留小数点1位
    LCD_Txt(5, 16, string_tmp); //压差
}



/******************************************************************************
  Function:     CollectQuantity_Display
  Description:  数据采集量排序显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void Home_Display(void)
{
    char string_tmp[50],stemp[20];
    float tp1,tp2;
//SetT,bandwidth,MaxvAntilation,MinvAntilation  Run_F,Antilation Refresh

    sprintf(string_tmp, "%.1f", ADC_Value[4]);//保留小数点1位
    LCD_Txt(0, 2, string_tmp); //温度
    sprintf(string_tmp, "%.1f", ADC_Value[5]);//保留小数点1位
    LCD_Txt(0, 3, string_tmp); //湿度

//	if(ADC_Value[4] < g_tParam.SetT)
//	{
//		sprintf(string_tmp, "%.1f", g_tParam.MinVntilation);//保留小数点1位
//	  LCD_Txt(0, 4, string_tmp); //当前通风量
//	}
//	else if(ADC_Value[4]>(g_tParam.SetT+g_tParam.bandwidth))
//	{
//		sprintf(string_tmp, "%.1f", g_tParam.MaxVntilation);//保留小数点1位
//	  LCD_Txt(0, 4, string_tmp); //当前通风量
//	}
//	else
//	{
    sprintf(string_tmp, "%.1f", Antilation);//保留小数点1位
    LCD_Txt(0, 4, string_tmp); //当前通风量
//	}


    if(PrintGPS_Flag)
    {
        tp1=gpsx.longitude;
        tp2=gpsx.latitude;
        if(gpsx.ewhemi==0x45)  sprintf(string_tmp, "%.4fE, ", tp1/100000);//保留小数点4位
        if(gpsx.ewhemi==0x57)  sprintf(string_tmp, "%.4fW, ", tp1/100000);//保留小数点4位
//	  LCD_Txt(0, 10, string_tmp); //经度

        if(gpsx.nshemi==0x4E) sprintf(stemp, "%.4fN", tp2/100000);//保留小数点4位
        if(gpsx.nshemi==0x53) sprintf(stemp, "%.4fS", tp2/100000);//保留小数点4位
        strcat(string_tmp,stemp);
        LCD_Txt(0, 10, string_tmp); ////经度 纬度
    }
    else
    {
        LCD_Txt(0, 10, "----");
    }

}

/******************************************************************************
  Function:     Alarm_Display
  Description:  报警信息显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void Alarm_Display(void)
{
    if(g_tParam.T_Abnormal==1)
    {
        LCD_Txt(2, 6, "开启");
        if((ADC_Value[4]<g_tParam.SetT)&&(Alarm_Reset==1))
        {
            LCD_Txt(2, 14, "房间温度过低");
        }
        else if((ADC_Value[4]>(g_tParam.SetT+g_tParam.bandwidth))&&(Alarm_Reset==1))
        {
            LCD_Txt(2, 14, "房间温度过高");
        }
        else
        {
            LCD_Txt(2, 14, "");
        }

    }
    if(g_tParam.T_Difference==1)
    {
        LCD_Txt(2, 7, "开启");
        if((T_Difference_Status==1)&&(Alarm_Reset==1))
            LCD_Txt(2, 15, "T_Difference_Alarm");
        else
            LCD_Txt(2, 15, "");
    }

    if(g_tParam.CO2_Alarm==1)
    {
        LCD_Txt(2, 8, "开启");
        if((CO2_Alarm_Status==1)&&(Alarm_Reset==1))
            LCD_Txt(2, 16, "CO2浓度过高");
        else
            LCD_Txt(2, 16, "");
    }
    if(g_tParam.Fan_Failure==1)
    {
        LCD_Txt(2, 9, "开启");
        if((Fan_Status==1)&&(Alarm_Reset==1))
            LCD_Txt(2, 17, "风机故障");
        else
            LCD_Txt(2, 17, "");
    }

    if(g_tParam.T_Abnormal==2)  LCD_Txt(2, 6, "关闭");
    if(g_tParam.T_Difference==2)  LCD_Txt(2, 7, "关闭");
    if(g_tParam.CO2_Alarm==2)  LCD_Txt(2, 8, "关闭");
    if(g_tParam.Fan_Failure==2)  LCD_Txt(2, 9, "关闭");

    if(Alarm_Reset==0)
    {
        LCD_Txt(2, 14, "");
        LCD_Txt(2, 15, "");
        LCD_Txt(2, 16, "");
        LCD_Txt(2, 17, "");
    }

}

/******************************************************************************
  Function:     grow_curve_Display
  Description:  生长曲线参数显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void grow_curve_Display(void)
{
    char string_tmp[50];

    int temp;
    temp=g_tParam.pig_in_date.Date + g_tParam.pig_in_date.Month * 100 + ( g_tParam.pig_in_date.Year+2000 ) * 10000;
    sprintf(string_tmp, "%d", temp);  //进猪日期
    LCD_Txt(8, 25, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.pig_Breeding_date);//生长天数
    LCD_Txt(8, 26, string_tmp);

    sprintf(string_tmp, "%d", g_tParam.grow_curve[0][0]);//1点 时间节点
    LCD_Txt(8, 2, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[0][1]);//1点 最小通风
    LCD_Txt(8, 3, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[0][2]);//1点 最大通风
    LCD_Txt(8, 4, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[0][3]);//1点 房间温度
    LCD_Txt(8, 5, string_tmp);
    HAL_IWDG_Refresh(&hiwdg);

    sprintf(string_tmp, "%d", g_tParam.grow_curve[1][0]);//2点 时间节点
    LCD_Txt(8, 6, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[1][1]);//2点 最小通风
    LCD_Txt(8, 7, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[1][2]);//2点 最大通风
    LCD_Txt(8, 8, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[1][3]);//2点 房间温度
    LCD_Txt(8, 9, string_tmp);
    HAL_IWDG_Refresh(&hiwdg);

    sprintf(string_tmp, "%d", g_tParam.grow_curve[2][0]);//3点 时间节点
    LCD_Txt(8, 10, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[2][1]);//3点 最小通风
    LCD_Txt(8, 11, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[2][2]);//3点 最大通风
    LCD_Txt(8, 12, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[2][3]);//3点 房间温度
    LCD_Txt(8, 13, string_tmp);
    HAL_IWDG_Refresh(&hiwdg);

    sprintf(string_tmp, "%d", g_tParam.grow_curve[3][0]);//4点 时间节点
    LCD_Txt(8, 14, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[3][1]);//4点 最小通风
    LCD_Txt(8, 15, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[3][2]);//4点 最大通风
    LCD_Txt(8, 16, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[3][3]);//4点 房间温度
    LCD_Txt(8, 17, string_tmp);
    HAL_IWDG_Refresh(&hiwdg);

    sprintf(string_tmp, "%d", g_tParam.grow_curve[4][0]);//5点 时间节点
    LCD_Txt(8, 18, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[4][1]);//5点 最小通风
    LCD_Txt(8, 19, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[4][2]);//5点 最大通风
    LCD_Txt(8, 20, string_tmp);
    sprintf(string_tmp, "%d", g_tParam.grow_curve[4][3]);//5点 房间温度
    LCD_Txt(8, 21, string_tmp);
    if(g_tParam.grow_curve_Enable==1)   //生长曲线使能
    {
        LCD_Txt(8, 23, "开启");
    }
    else
    {
        LCD_Txt(8, 23, "关闭");
    }
}


/******************************************************************************
  Function:     ServerPa_Display
  Description:  服务器参数显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void ServerPa_Display(void)
{

}

/******************************************************************************
  Function:     CEMS_Display
  Description:  烟气参数显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void CEMS_Display(void)
{

}

/******************************************************************************
  Function:     BasicP_Display
  Description:  数采仪基本参数显示
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void BasicP_Display(void)
{
    char string_tmp[20];

    sprintf(string_tmp, "%d", g_tParam.u8_ST);//保留小数点1位
    LCD_Txt(3, 4, string_tmp); //温度
}


/******************************************************************************
  Function:     ServerPa_Save
  Description:  服务器参数存储
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void ServerPa_Save(void)
{
   uint8_t j;

	if(Server_flag&0x0001)  //1#ip
	{
		j=0;
		for(j=0;j<4;j++)
		{
			 g_tParam.Srv[0][j]=Server_ip[0][j];
		}
	}
	if(Server_flag&0x0002) //1#端口
	{
		   g_tParam.Srv[0][4]=Server_ip[0][4];
	}
	if(Server_flag&0x0004)  //1#实时周期
	{
		   g_tParam.Srv[0][5]=Server_ip[0][5];
	}

	if(Server_flag&0x0008)  //2#ip
	{
		j=0;
		for(j=0;j<4;j++)
		{
			 g_tParam.Srv[1][j]=Server_ip[1][j];
		}
	}
	if(Server_flag&0x0010) //2#端口
	{
		   g_tParam.Srv[1][4]=Server_ip[1][4];
	}
	if(Server_flag&0x0020)  //2#实时周期
	{
		   g_tParam.Srv[1][5]=Server_ip[1][5];
	}

	if(Server_flag&0x0040)  //3#ip
	{
		j=0;
		for(j=0;j<4;j++)
		{
			 g_tParam.Srv[2][j]=Server_ip[2][j];
		}
	}
	if(Server_flag&0x0080) //3#端口
	{
		   g_tParam.Srv[2][4]=Server_ip[2][4];
	}
	if(Server_flag&0x0100)  //3#实时周期
	{
		   g_tParam.Srv[2][5]=Server_ip[2][5];
	}

	if(Server_flag&0x0200)  //4#ip
	{
		j=0;
		for(j=0;j<4;j++)
		{
			 g_tParam.Srv[3][j]=Server_ip[3][j];
		}
	}
	if(Server_flag&0x0400) //4#端口
	{
		   g_tParam.Srv[3][4]=Server_ip[3][4];
	}
	if(Server_flag&0x0800)  //4#实时周期
	{
		   g_tParam.Srv[3][5]=Server_ip[3][5];
	}

  if(Server_flag)
	{
		SaveParam();                        /* 将新参数写入Flash */
    LoadParam();
		ServerPa_Display();
		Server_flag=0;
		LCD_Txt(4, 16, "参数设置成功");
	}
	if(ServerRest_Flag == 1)
	{
		 ServerRest_Flag=2;      //跳转到Task_GPRS任务中重启GPRS
	}
}



/******************************************************************************
  Function:     UpdateLcdDisplay
  Description:  更新串口显示屏的显示内容
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void UpdateLcdDisplay(void)
{


    switch (LCD_ScreenId)
    {
    case 0: //更新串口显示屏相应画面的显示信息
        UpdateLcdTime();    //更新显示屏的时钟
		   Home_Display();   //更新监测参数浓度值
        break;
    case 1:
        VentilationCurve_Display();   //通风曲线数据
        break;
		case 2:
        Alarm_Display();      //报警信息显示
        break;
    case 3:
		BasicP_Display();   //数采仪基本参数
        break;
		case 5:
        Debug_Display();     //调试窗口显示
        break;
		case 8:
        grow_curve_Display();   //生长曲线
        break;
    default:
        break;
    }
}

/******************************************************************************
  Function:     UpdateLcdTime
  Description:  更新串口显示屏的时钟,日期
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void UpdateLcdTime(void)
{
    static int last_sec = 0;            //保存上一次秒的读数,用于检测RTC是否停止
    static uint8_t cnt = 0;             //用于检测RTC是否停止

    char string_tmp[30];//应该只需要20B

    HAL_RTC_GetTime(&hrtc, &TimeCurrent, RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc, &DateCurrent, RTC_FORMAT_BIN);
    if (last_sec == TimeCurrent.Seconds)
    {
        if (++cnt > 5)
        {
            //重新配置RTC
//          RTC_Configuration();
//          //配置完成后，向后备寄存器中写特殊字符0xA5A5
//          BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
//          Time_SetCalendarTime(time_now);

//          HAL_RTC_Init(&hrtc);
            cnt = 0;
        }
    }
    else
    {
        cnt = 0; //后来添加的,原来的有风险
    }
    last_sec = TimeCurrent.Seconds;

    sprintf(string_tmp, "%4d-%02d-%02d %02d:%02d:%02d", DateCurrent.Year + 2000, DateCurrent.Month,\
                DateCurrent.Date, TimeCurrent.Hours, TimeCurrent.Minutes, TimeCurrent.Seconds);

    LCD_Txt(0, 11, string_tmp);                             //更新时钟显示
}

#endif

/************** end file line ********************/

